//
//  MainTabBarController.swift
//  HinsLive
//
//  Created by haoxian on 2019/4/16.
//  Copyright © 2019 Hins. All rights reserved.
//

import UIKit
import SnapKit

final class MainTabBarController: UITabBarController {
    
    private var tabBarItemXoffset: CGFloat {
        return tabBar.width / 22
    }
    
    private lazy var homeVc: HomeViewController = {
        let vc = HomeViewController()
        vc.tabBarItem = UITabBarItem(title: "主页", image: R.image.home_n(), selectedImage: R.image.home_s())
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: -tabBarItemXoffset, vertical: 0)
        return vc
    }()
    
    private var liveInfoVc: RoomInfoViewController?
    
    private lazy var personalVc: UINavigationController = {
        let nav = UINavigationController(rootViewController: PersonalViewController())
        nav.isNavigationBarHidden = true
        nav.tabBarItem = UITabBarItem(title: "个人中心", image: R.image.mine_n(), selectedImage: R.image.mine_s())
        nav.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: tabBarItemXoffset, vertical: 0)
        return nav
    }()
    
    private(set) lazy var liveButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage(R.image.live_play(), for: .normal)
        btn.setBackgroundImage(R.image.live_play(), for: .highlighted)
        btn.addTarget(self, action: #selector(MainTabBarController.liveDidTap(_:)), for: .touchUpInside)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildViewControllers()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // 自定义view添加到tabbar上不能写在viewdidload中，要写在这里，否者无法响应点击
        setupSubviews()
    }
    
}

private extension MainTabBarController {
    func setupChildViewControllers() {
        addChild(homeVc)
        addChild(personalVc)
        viewControllers?.compactMap{ $0.tabBarItem }.forEach {
            $0.setTitleTextAttributes([.foregroundColor : UIColor.tabbarTitleNormalColor], for: .normal)
            $0.setTitleTextAttributes([.foregroundColor : UIColor.tabbarTitleSelectedColor], for: .selected)
        }
    }
    
    func setupSubviews() {
        tabBar.addSubview(liveButton)
        liveButton.snp.makeConstraints { (make) in
            make.bottom.equalTo(tabBar.safeAreaLayoutGuide.snp.bottom)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(64)
        }
    }
    
    @objc func liveDidTap(_ button: UIButton) {
        guard let window = UIApplication.shared.keyWindow else { return }
        guard AccountManager.share.isLogin else {
            let top = UIViewController.topViewController()
            top.view.makeCenterMsg("请先登录")
            return
        }
        guard let category = MemoryCache.default.value(type: FeedsCategory.self, for: MemoryCache.feedCategoryKey) else {
            let top = UIViewController.topViewController()
            top.view.makeCenterMsg("缺少直播列表信息")
            return
        }
        let vc = RoomInfoViewController(category: category)
        vc.cancelClosure = { [weak vc, weak self] in
            vc?.view.removeFromSuperview()
            self?.liveInfoVc = nil
        }
        vc.submitClosure = { [weak vc, weak self] roomInfo in
            vc?.view.removeFromSuperview()
            guard let self = self else { return }
            self.liveInfoVc = nil
            let liveVC = MasterLiveViewController(roomModel: roomInfo)
            self.present(liveVC, animated: true)
        }
        self.liveInfoVc = vc
        window.addSubview(vc.view)
    }
}
